//
//  ViewController.m
//  coolkeys
//
//  Created by yww on 31/07/2017.
//  Copyright © 2017 yww. All rights reserved.
//

#import "ViewController.h"
#import <ImageIO/ImageIO.h> // 图像的输入输出文件
#import <MobileCoreServices/MobileCoreServices.h>
@interface ViewController ()
@property (nonatomic, strong) NSArray  *animationImages;
@property (nonatomic, assign) int keyBoardHeight;
@property (nonatomic, assign) BOOL isIpad;
@property (nonatomic, strong) UIImage *gifImage;
@end

@implementation ViewController

- (NSArray *)animationImages{
    if (!_animationImages) {
        NSMutableArray *imagesArr = [NSMutableArray array];
        for (int i = 0;i <43;i ++) {
            UIImage *image = [UIImage imageNamed: [NSString stringWithFormat:@"skulls-%d",i+1]];
            if (image) {
                [imagesArr addObject:image];
            }
        }
        _animationImages = imagesArr;
    }
    return _animationImages;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){
        self.isIpad = YES;
    }else{
        self.isIpad = NO;
    }
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    self.view.backgroundColor = [UIColor blackColor];
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake( 50, 80,(KdeviceWidth-100), self.isIpad?30:20)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.text = @"CoolKey";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont systemFontOfSize:self.isIpad?25:18];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:titleLabel];
    
    
    
    /*
    UIImageView *gifImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 200, KdeviceWidth, KdeviceWidth)];
    gifImageView.layer.cornerRadius = 10;
    gifImageView.clipsToBounds = YES;
    gifImageView.animationImages = self.animationImages; //获取Gif图片列表
    gifImageView.animationDuration = 5;     //执行一次完整动画所需的时长
    gifImageView.animationRepeatCount = 0;  //动画重复次数
    [gifImageView startAnimating];
    [self.view addSubview:gifImageView];
     */
    
    // 读取gif图片数据 注意:传入nil参数可能有警告
    NSString *path = [[NSBundle mainBundle] pathForResource:@"fireMen" ofType:@"gif"];
    // 获取gif尺寸
    [self didCompositionGif:path];
    CGFloat fixelW;
    CGFloat fixelH;
    if (self.gifImage) {
       fixelW = CGImageGetWidth(self.gifImage.CGImage);
       fixelH = CGImageGetHeight(self.gifImage.CGImage);
    }else{
       fixelW = KdeviceWidth;
       fixelH = KdeviceWidth *2/3.0;
    }
    NSData *data = [NSData dataWithContentsOfFile: path];
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(self.isIpad?(KdeviceWidth-fixelW)/2.0:0,self.isIpad?250:180,fixelW,self.isIpad?fixelH:KdeviceWidth *2/3.0)];
    webView.backgroundColor = [UIColor blackColor];
    //加载
    [webView loadData:data MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
    [self.view addSubview:webView];
    
    // 增加监听，当键接收消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    //增加监听，当键退出时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];

}

//获取图片尺寸
- (void)didCompositionGif:(NSString *)path {
    //1. 拿到gif数据
    NSData * data = [NSData dataWithContentsOfFile:path];
    //#warning 桥接的意义 (__bridge CFDataRef)
    CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
    //2. 将gif分解为一帧帧
    size_t count = CGImageSourceGetCount(source);

    for (int i = 0; i < count; i ++) {
        CGImageRef imageRef = CGImageSourceCreateImageAtIndex(source, i, NULL);
        //3. 将单帧数据转为UIImage
        UIImage * image = [UIImage imageWithCGImage:imageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
        if (image) {
            self.gifImage = image;
            //#warning CG类型的对象 不能用ARC自动释放内存.需要手动释放
            CGImageRelease(imageRef);
            CFRelease(source);
            return;
        }
    }
    
}

//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification{
    //获取键盘的高度
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    self.keyBoardHeight = keyboardRect.size.height;
//    NSLog(@"键盘高度==%d", self.keyBoardHeight);
}

//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification{
    
}
- (void)btnClick:(UIButton *)btn{

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
